dojo.subscribe("LoginSuccessful", function() {
   window.location.reload(true);
});

dojo.addOnLoad(function () {
    var openProject = getQueryStringParameter("openProject");
    if (openProject) {
        openOrSelectTab(openProject, getQueryStringParameter("projectAlias"), "project");
        var openIssue = getQueryStringParameter("openIssue");
        if (openIssue) {
            openOrSelectTab(openIssue, getQueryStringParameter("issueAlias"), "issue");
        }
    }
});

function getQueryStringParameter(param) {
    var queryParams = dojo.queryToObject(window.location.search.slice(1));
    return queryParams[param];
}

function createBookmark(url, title) {
    if (window.sidebar) {
        window.sidebar.addPanel(title, url, "");
    } else if (window.opera && window.print) {
        var e=document.createElement('a');
        e.setAttribute('href',url);
        e.setAttribute('title',name);
        e.setAttribute('rel', 'sidebar');
        e.click();
    } else if( window.external ) {
        window.external.AddFavorite(url, title);
    }
}

function openAndClearPollAdminForm() {
    openPopup("createAdminPoll");
    dijit.byId("activeadminPoll").reset();
    byId("pollValidationWarningContainer").innerHTML = "";
    setPOLLData({path: "global_adminPoller"}, null);
}

function saveAdminPoll() {
    var button = dijit.byId("createPollButton");
    var loading = byId("issueLinkLoadingContainer");
    button.setDisabled(true);
    dojo.style(loading, "display", "inline");
    byId("pollValidationWarningContainer").innerHTML = "";
    AdministratorService.managePoll(getPOLLData({path: "global_adminPoller"}), "on" == dijit.byId("activeadminPoll").getValue(), {
        callback: function(data) {
            button.setDisabled(false);
            dojo.style(loading, "display", "none");
            if (data) {
                byId("pollValidationWarningContainer").innerHTML = data;
            } else {
                closePopup("createAdminPoll");
                dojo.publish("PollFiltered", ["adminPollGrid", []]);
            }
        },
        errorHandler: function(errorString, exception) {
            button.setDisabled(false);
            dojo.style(loading, "display", "none");
            byId("pollValidationWarningContainer").innerHTML = errorString ? errorString : exception;
        }
    });
}

function editAdminPoll() {
    var grid = dijit.byId("gridadminPollGrid");
    if ((grid.selection.selectedIndex >= 0) && (grid.selection.getSelected()[0] != null)) {
        openAndClearPollAdminForm();
        dijit.byId("activeadminPoll").setValue(grid.selection.getSelected()[0].active[0]);
        setPOLLData({path: "global_adminPoller"}, {poll: [grid.selection.getSelected()[0]]});
    }
}

function toggleAdminPoll() {
    if (dojo.style(byId("spriteadminPollToggle"), "opacity") == "1") {
        var grid = dijit.byId("gridadminPollGrid");
        var poll = grid.selection.getSelected()[0].id[0];
        grid.selection.clear();
        dojo.style(byId("spriteadminPollEdit"), "opacity", "0.4");
        dojo.style(byId("spriteadminPollToggle"), "opacity", "0.4");
        AdministratorService.togglePoll(poll, function (data) {
            if (!data) dojo.publish("PollFiltered", ["adminPollGrid", []]);
        });
    }
}